//
//  NewsDetailView.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/11.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "NewsDetailView.h"
#import "XMLabel.h"
#import "UILabel+Extension.h"

@interface NewsDetailView ()<UIWebViewDelegate>
@property(nonatomic, strong)UIView *subView;
/// 文章副标题
@property(nonatomic, strong)XMLabel *subheadLabel;
@property(nonatomic, strong)UIWebView *webView;
@property(nonatomic, assign)CGFloat webHeight;
@end

@implementation NewsDetailView

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        self.subView = [[UIView alloc] init];
        self.subView.backgroundColor = [UIColor xm_colorFromRGB:0x5D646E alpha:0.04];
        [self addSubview:self.subView];

        
        self.subheadLabel = [[XMLabel alloc] init];
        self.subheadLabel.textColor = [UIColor xm_somberColor];
        self.subheadLabel.numberOfLines = 0;
        self.subheadLabel.font = [UIFont xm_font:15.0];
        [self.subView addSubview:self.subheadLabel];
        

        self.webView = [[UIWebView alloc] init];
        self.webView.scrollView.scrollEnabled = NO;
        self.webView.delegate = self;
        self.webView.backgroundColor = XMRandomColor;
        [self addSubview:self.webView];
    }
    return self;
}
-(void)setDetailModel:(AticleDetailModel *)detailModel{
    _detailModel = detailModel;
    self.subheadLabel.attributedText = [XMTool attributedWithString:_detailModel.ShortDescription font:[UIFont xm_font:15.0] lineSpace:5.0];

    [self.webView loadHTMLString:[self htmlEntityDecode:_detailModel.Remark] baseURL:nil];

}

- (NSString *)htmlEntityDecode:(NSString *)string{
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"
    
    string = [NSString stringWithFormat:@"<html> \n"
                "<head> \n"
                "<style type=\"text/css\"> \n"
                "body {font-size:15px;}\n"
                "</style> \n"
                "</head> \n"
                "<body>"
                "<script type='text/javascript'>"
                "window.onload = function(){\n"
                "var $img = document.getElementsByTagName('img');\n"
                "for(var p in  $img){\n"
                " $img[p].style.width = '100%%';\n"
                "$img[p].style.height ='auto'\n"
                "}\n"
                "}"
                "</script>%@"
                "</body>"
                "</html>",string];
    return string;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.webHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
//    webView.height = webHeight;

    NSLog(@"webHeight == %f",self.webHeight);
    [self setNeedsLayout];
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat textHeight = [XMTool getSpaceLabelHeight:self.detailModel.ShortDescription
                                            withFont:[UIFont xm_font:15.0]
                                           withWidth:self.width -70
                                           lineSpace:5];
    
    self.subView.frame = CGRectMake(20, 0, self.width - 40, textHeight + 40);
    self.subheadLabel.frame = CGRectMake(15, 20, self.width -70, textHeight);
    self.webView.frame =CGRectMake(0, CGRectGetMaxY(self.subView.frame), self.width, self.webHeight);
    
    if (self.heightBlock) {
        self.heightBlock(CGRectGetMaxY(self.subView.frame) + self.webHeight);
    }
}
-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
}


@end
